class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        sort(nums.begin(), nums.end());

        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] > 0) break;

            if (i == 0 || i > 0 && nums[i] != nums[i - 1])
            {
                int left = i + 1, right = nums.size() - 1;
                while (left < right)
                {
                    int sum = nums[left] + nums[right];
                    if (sum < -nums[i]) left++;
                    else if (sum > -nums[i]) right--;
                    else
                    {
                        vv.push_back({ nums[i],nums[left],nums[right] });
                        left++;
                        right--;
                    }
                    while (left > i + 1 && left < right && nums[left] == nums[left - 1]) left++;
                    while (right < nums.size() - 1 && left < right && nums[right] == nums[right + 1]) right--;
                }
            }
        }

        return vv;
    }
};